package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.*;
import com.asm.vo.sys.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import javax.annotation.Resource;

/**
 * @author CrysTzj
 * @program AdvertisementManagementSystem
 * @description 广告位保障相关逻辑设计
 * @date  2019-07-11 00:09
 **/
@Api(value = "维修单Controller")
@Slf4j
@RestController
public class RepairController extends BaseController {
    @Resource
    private MaintenanceService maintenanceService;

    @Resource
    private WorkOrderService workOrderService;

    /**
     * 分页查询所有维修申请，查询结果按状态排序
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 分页并排序的维修申请
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNumber", value = "当前页码"), @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @GetMapping("/maintenances")
    public ResponseEntity<PageResult<MaintenanceVo>> getMaintenancesBySort(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", maintenanceService.getMaintenancesBySort(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据用户获取用户的报障列表
     *
     * @param customerId 用户id
     * @param pageNumber 页码
     * @param pageSize   页大小
     * @return 维修列表
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "customerId", value = "用户id"), @ApiImplicitParam(name = "pageNumber", value = "当前页码"), @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @GetMapping("/maintenance/customerId/{customerId}")
    public ResponseEntity<PageResult<MaintenanceVo>> getMaintenanceByCustomerId(@PathVariable("customerId") Long customerId, @RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", maintenanceService.findMaintenanceByCustomerId(customerId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据业务员编号获取报障列表
     *
     * @param sellId 业务员编号
     * @return 报障列表
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "sellId", value = "业务员编号")})
    @GetMapping("/maintenance/sellId/{sellId}")
    public ResponseEntity<Result> getMaintenanceBySellId(@PathVariable("sellId") Long sellId) {
        return new ResponseEntity(new Result().ok("success", maintenanceService.findMaintenanceBySellId(sellId)), HttpStatus.OK);
    }

    /**
     * 发送报障维修信息并生成工单
     *
     * @param maintenanceVo 损坏的广告位信息
     * @return 响应状态信息
     */
    @PostMapping("/maintenance/apply")
    public ResponseEntity<Result> repairApply(@RequestBody @ApiParam(name = "maintenanceVo", value = "损坏的广告位信息") MaintenanceVo maintenanceVo) {
        maintenanceService.repairApply(maintenanceVo);
        return new ResponseEntity<>(new Result().ok("create maintenance information success"), HttpStatus.OK);
    }

    /**
     * 管理员派发维修申请
     *
     * @param maintenanceVo 维修申请信息
     * @return 成功信息
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "maintenanceVo", value = "维修申请信息")})
    @PostMapping("/maintenance/assignTask")
    public ResponseEntity<Result> assignTask(@RequestBody @ApiParam(name = "maintenanceVo", value = "损坏的广告位信息") MaintenanceVo maintenanceVo) {
        if (maintenanceVo.getWorkerOrderId() != null) {
            return new ResponseEntity<>(new Result().serverError("已经派发任务了"), HttpStatus.OK);
        }
        maintenanceService.assignTask(maintenanceVo);
        return new ResponseEntity<>(new Result().ok("成功派发任务"), HttpStatus.OK);
    }

}
